#include "Queue.h"

void QueueTest1()
{
	Queue q;

	QueueInit(&q);
	QueuePush(&q, 1);
	QueuePush(&q, 2);

	printf("%d ", QueueFront(&q));
	QueuePop(&q);

	QueuePush(&q, 3);
	QueuePush(&q, 4);

	printf("%d\n", QueueSize(&q));
	printf("%d\n", QueueEmpty(&q));
	printf("%d\n", QueueFront(&q));
	printf("%d\n", QueueBack(&q));

	while (!QueueEmpty(&q))
	{
		printf("%d ", QueueFront(&q));
		QueuePop(&q);
	}
	printf("\n");

	printf("%d\n", QueueSize(&q));
	printf("%d\n", QueueEmpty(&q));
	//printf("%d\n", QueueFront(&q));
	//printf("%d\n", QueueBack(&q));

	QueueDestroy(&q);

}

typedef struct
{
    Queue q1;
    Queue q2;

} MyStack;


MyStack* myStackCreate()
{
    MyStack* obj = (MyStack*)malloc(sizeof(MyStack));

    QueueInit(&obj->q1);
    QueueInit(&obj->q2);
    return obj;
}

void myStackPush(MyStack* obj, int x)
{
    assert(obj);

    if (!QueueEmpty(&obj->q1))
    {
        QueuePush(&obj->q1, x);
    }
    else
    {
        QueuePush(&obj->q2, x);
    }
}

int myStackPop(MyStack* obj)
{
    Queue* emptyQ = &obj->q1;
    Queue* noemptyQ = &obj->q2;

    if (!QueueEmpty(emptyQ))
    {
        emptyQ = &obj->q2;
        noemptyQ = &obj->q1;
    }

    while (QueueSize(noemptyQ) > 1)
    {
        QueuePush(emptyQ, QueueFront(noemptyQ));
        QueuePop(noemptyQ);
    }

    int top = QueueFront(noemptyQ);
    QueuePop(noemptyQ);

    return top;
}

int myStackTop(MyStack* obj)
{
    if (!QueueEmpty(&obj->q1))
    {
        return QueueBack(&obj->q1);
    }
    else
    {
        return QueueBack(&obj->q2);
    }
}

bool myStackEmpty(MyStack* obj)
{
    return QueueEmpty(&obj->q1) && QueueEmpty(&obj->q2);
}

void myStackFree(MyStack* obj)
{
    QueueDestory(&obj->q1);
    QueueDestory(&obj->q2);
    free(obj);
}

int main()
{
	QueueTest1();

	return 0;
}